<?php
/**
 * @title 376. 摆动序列 - 波峰波谷
 * @author start2004
 */

class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer
     */
    function wiggleMaxLength($nums) {
        /**
         * @since 2020-08-03 长度
         */
        $len = count($nums);

        /**
         * @since 2020-08-03 极端情况
         */
        if($len<1){
            return 0;
        } else {}

        /**
         * @since 2020-08-03 摆动序列长度
         */
        $wiggleLen = 1;

        /**
         * @since 2020-08-03 正负方向, true=正, false=负
         */
        $direction = null;

        /**
         * @since 2020-08-03 上一个数
         */
        $prevNum = $nums[0];

        /**
         * @since 2020-08-03 遍历
         */
        for($i=1; $i<$len; $i++){
            /**
             * @since 2020-08-03 升序
             */
            if($nums[$i] > $prevNum){
                if($direction === null OR $direction === true){
                    $wiggleLen++;
                    $direction = false;
                    $prevNum = $nums[$i];
                } else {
                    /**
                     * @since 2020-08-03 方向负数, 换个大数, 更容易找到比它小的
                     */
                    $prevNum = $nums[$i];
                }
            } elseif($nums[$i] < $prevNum) {
                /**
                 * @since 2020-08-03 降序
                 */
                if($direction === null OR $direction === false){
                    $wiggleLen++;
                    $direction = true;
                    $prevNum = $nums[$i];
                } else {
                    $prevNum = $nums[$i];
                }
            } else {}
        }

        /**
         * @return
         */
        return $wiggleLen;
    }
}




/**
 * @url http://127.0.0.1/leetcode/202008/2020.08.03_3.php
 */
$datas = [
    [1,7,4,9,2,5],
    [1,1,1,],
    [1,17,5,10,13,15,10,5,16,8],
    [1,2,3,4,5],
    [1,1,1,1,2],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->wiggleMaxLength($data);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
